import type { ActionContext } from "vuex";
import type { Counter, RootState } from "../types";

export default {
  // 开启命名空间
  namespaced: true,
  // 定义数据
  state: {
    count: 0,
  },
  // 定义只读计算属性数据
  getters: {
    oddOrEven(state: Counter) {
      return state.count % 2 === 1 ? "奇数" : "偶数";
    },
  },
  // 定义包含异步操作方法
  actions: {
    increment({ commit }: ActionContext<Counter, RootState>, val: number) {
      setTimeout(() => {
        commit("INCREMENT", val);
      }, 1000);
    },
  },
  // 定义对state数据进行直接更新的方法
  mutations: {
    INCREMENT(state: Counter, val: number) {
      state.count += val;
    },
    DECREMENT(state: Counter, val: number) {
      state.count -= val;
    },
  },
};
